{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "agmT3hrjsffX"
      },
      "source": [
        "# Gemini API: JSON Mode Quickstart with REST\n",
        "\n",
        "<table align=\"left\">\n",
        "  <td>\n",
        "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/gemini-api-cookbook/blob/main/quickstarts/rest/JSON_mode_REST.ipynb\"><img src=\"https://www.tensorflow.org/images/colab_logo_32px.png\" />Run in Google Colab</a>\n",
        "  </td>\n",
        "</table>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "JMNKdTpTGZET"
      },
      "source": [
        "This notebook provides a code example that shows you how to get started with JSON mode using `curl`.\n",
        "\n",
        "You can run this in Google Colab, or you can copy/paste the `curl` commands into your terminal.\n",
        "\n",
        "To run this notebook, your API key must be stored it in a Colab Secret named GOOGLE_API_KEY. If you are running in a different environment, you can store your key in an environment variable. See [Authentication](https://github.com/google-gemini/gemini-api-cookbook/blob/main/quickstarts/Authentication.ipynb) to learn more."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "R-Vw_mOM_WD0"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "from google.colab import userdata"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "wCkLTpb3oTXE"
      },
      "outputs": [],
      "source": [
        "os.environ['GOOGLE_API_KEY'] = userdata.get('GOOGLE_API_KEY')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "tjGqGBZ9yARd"
      },
      "source": [
        "## Activate JSON Mode\n",
        "\n",
        "To activate JSON mode, set `response_mime_type` to `application/json` in the `generationConfig`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "eA7I_Ww8IETn"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{\n",
            "  \"candidates\": [\n",
            "    {\n",
            "      \"content\": {\n",
            "        \"parts\": [\n",
            "          {\n",
            "            \"text\": \"[{\\\"recipe_name\\\":\\\"Chocolate Chip Cookies\\\"},{\\\"recipe_name\\\":\\\"Peanut Butter Cookies\\\"},{\\\"recipe_name\\\":\\\"Oatmeal Raisin Cookies\\\"},{\\\"recipe_name\\\":\\\"Sugar Cookies\\\"},{\\\"recipe_name\\\":\\\"Shortbread Cookies\\\"}] \\n\"\n",
            "          }\n",
            "        ],\n",
            "        \"role\": \"model\"\n"
          ]
        }
      ],
      "source": [
        "%%bash\n",
        "\n",
        "curl https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro-latest:generateContent?key=$GOOGLE_API_KEY \\\n",
        "-H 'Content-Type: application/json' \\\n",
        "-d '{\n",
        "    \"contents\": [{\n",
        "      \"parts\":[\n",
        "        {\"text\": \"List a few popular cookie recipes using this JSON schema:\n",
        "          {'type': 'object', 'properties': { 'recipe_name': {'type': 'string'}}}\"\n",
        "          }\n",
        "        ]\n",
        "    }],\n",
        "    \"generationConfig\": {\n",
        "        \"response_mime_type\": \"application/json\",\n",
        "    }\n",
        "}' 2> /dev/null | head"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "OxN68aKNDxEV"
      },
      "source": [
        "To turn off JSON mode, set `response_mime_type` to `text/plain` (or omit the `response_mime_type` parameter)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "jXkRYBhbB_b2"
      },
      "source": [
        "## Learning more\n",
        "\n",
        "See the [JSON mode documentation](https://ai.google.dev/docs/gemini_api_overview#json) and the [REST API reference](https://ai.google.dev/api/rest/v1beta/GenerationConfig) for `generationConfig` to learn more.\n"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "JSON_mode_REST.ipynb",
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
